package com.shujia.filter;

import com.shujia.utils.HbaseTool;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseZuheFilter {
    public static void main(String[] args) throws Exception {
        xuQiu1();
    }

    public static void xuQiu1() throws Exception{
        //运用4种比较器过滤出姓于，年纪大于23岁，性别为女，且是理科的学生
        //获取表对象
        Table students2 = HbaseTool.getOneTable("students2");
        Scan scan = new Scan();



        SingleColumnValueFilter filter1 = new SingleColumnValueFilter(
                Bytes.toBytes("info"),
                Bytes.toBytes("name"),
                CompareOperator.EQUAL,
                new BinaryPrefixComparator(Bytes.toBytes("于"))
        );

        SingleColumnValueFilter filter2 = new SingleColumnValueFilter(
                Bytes.toBytes("inter"),
                Bytes.toBytes("age"),
                CompareOperator.GREATER,
                new BinaryComparator(Bytes.toBytes("20"))
        );

        SingleColumnValueFilter filter3 = new SingleColumnValueFilter(
                Bytes.toBytes("hadoop"),
                Bytes.toBytes("gender"),
                CompareOperator.EQUAL,
                new BinaryComparator(Bytes.toBytes("女"))
        );

        SingleColumnValueFilter filter4 = new SingleColumnValueFilter(
                Bytes.toBytes("oppo"),
                Bytes.toBytes("clazz"),
                CompareOperator.EQUAL,
                new SubstringComparator("理科")
        );

        FilterList filterList = new FilterList();
        filterList.addFilter(filter1);
        filterList.addFilter(filter2);
        filterList.addFilter(filter3);
        filterList.addFilter(filter4);

        //设置过滤器
        //Scan setFilter(Filter filter)
        scan.setFilter(filterList);

        ResultScanner studentsScanner = students2.getScanner(scan);
        for (Result rs : studentsScanner) {
            HbaseTool.printResult(rs);
        }

        HbaseTool.close();

    }
}
